<template>
  <div class="app-container">
    <el-form :inline="true" label-width="80px">
      <el-form-item label="手机号码">
        <el-input
          v-model="queryParams.mobile"
          placeholder="手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称/姓名">
        <el-input
          v-model="queryParams.nickname"
          placeholder="昵称/姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select style="width: 100px" v-model="queryParams.status" placeholder="状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="角色">
        <el-select style="width: 100px" v-model="queryParams.role" placeholder="角色" clearable size="small">
          <el-option
            v-for="dict in roleOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker
          v-model="startTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="用户编号" align="center" prop="idNo"/>
      <el-table-column label="头像" align="center" prop="avatar">
        <template slot-scope="scope">
          <div>
            <el-popover placement="right" title="" trigger="click">
              <img :src="scope.row.avatar" alt="">
              <img slot="reference" :src="scope.row.avatar" alt="" style="max-height: 50px;max-width: 50px">
            </el-popover>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickname"/>
      <el-table-column label="真实姓名" align="center" prop="realName">
        <template slot-scope="scope">
          <span :class="scope.row.realName?'':'text-warning'">{{ scope.row.realName?scope.row.realName:'未实名' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="mobile"/>
      <el-table-column label="推荐人" align="center">
        <template slot-scope="scope">
          <el-link v-if="scope.row.inviteNickname" title="查看详情" type="primary"
                   @click="referenceInfo(scope.row.inviteId)">{{scope.row.inviteNickname}}
          </el-link>
          <span v-else class="text-warning">暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="gender" :formatter="genderFormat"/>
      <el-table-column label="角色" align="center" prop="role" :formatter="roleFormat"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="创建时间" align="center" prop="createtime" width="180">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.createtime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.makerId"
            size="mini"
            type="text"
            @click="handleInsert(scope.row)"
            v-hasPermi="['maker:list:add']"
          >添加创客
          </el-button>
          <el-tag
            v-if="scope.row.makerId"
            type="success"
            effect="dark">
            已是创客
          </el-tag>
          <el-button
            size="mini"
            type="text"
            @click="handleOffLine(scope.row)"
            v-hasPermi="['main:user:offline']"
          >强制下线
          </el-button>
        </template>

      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="总价">
          <span class="text-info">{{form.singleamount*form.number}}元</span>
        </el-form-item>

        <el-form-item label="单价(元)" prop="singleamount">
          <el-input-number v-model="form.singleamount" :precision="2" :step="150" :disabled="true"></el-input-number>
        </el-form-item>

        <el-form-item label="数量(张)" prop="number">
          <el-input-number v-model="form.number" :step="1" :max="999" :min="1"></el-input-number>
        </el-form-item>

        <el-form-item label="推荐人(线上)">
          <span :class="form.inviteNickname?'':'text-warning'">{{ form.inviteNickname?form.inviteNickname+'-'+form.inviteRole+'-'+form.inviteMobile:'暂无' }}</span>
        </el-form-item>

        <el-form-item label="推荐人(线下)" prop="inviteUserId">
          <el-select
            style="width: 300px;"
            v-model="form.inviteUserId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入手机号码查询"
            :remote-method="remoteMethod"
            :loading="selectLoading">
            <el-option
              v-for="item in options"
              :key="item.idNo"
              :label="item.nickname+'-'+roleFormat(item)+'-'+item.mobile"
              :value="item.userId">
              <span style="float: left">{{ item.mobile }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ roleFormat(item) }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="userInfoOpen" width="600px">
      <el-form ref="form" label-width="150px">
        <el-form-item label="用户编号:">
          {{user.idNo}}
        </el-form-item>
        <el-form-item label="昵称:">
          {{user.nickname}}
        </el-form-item>
        <el-form-item label="真实姓名:">
          <span :class="user.realName?'':'text-warning'">{{ user.realName?user.realName:'未实名' }}</span>
        </el-form-item>
        <el-form-item label="手机号码:">
          {{user.mobile}}
        </el-form-item>
        <el-form-item label="角色:">
          {{roleFormat(user)}}
        </el-form-item>
        <el-form-item label="性别:">
          {{genderFormat(user)}}
        </el-form-item>
        <el-form-item label="创建时间:">
          {{ dateFormat(user.createtime) }}
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
  import {list, addMaker,offline} from "@/api/maker/user";

  export default {
    data() {
      return {
        options: [],
        startTime: '',
        // 遮罩层
        loading: false,
        selectLoading: false,
        // 总条数
        total: 0,
        // 岗位表格数据
        postList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        userInfoOpen: false,
        // 状态数据字典
        statusOptions: [],
        genderOptions: [],
        roleOptions: [],
        user: {},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          mobile: undefined,
          nickname: undefined,
          status: undefined,
          startTime: undefined,
          endTime: undefined,
          role: undefined
        },
        form: {
          singleamount: 150,
          userId: undefined,
          number: 1,
          role: undefined,
          mobile: undefined,
          inviteUserId: undefined,
          inviteNickname: undefined
        },
        rules: {
          singleamount: [
            {required: true}
          ],
          number: [
            {required: true}
          ],
          inviteUserId:[
            {required: true,message:'线下推荐人必填'}
          ]
        }
      };
    },
    created() {
      this.getDicts("sys_user_sex").then(response => {
        this.genderOptions = response.data;
      });

      this.getDicts("yy_user_status").then(response => {
        this.statusOptions = response.data;
      });

      this.getDicts("yy_user_role").then(response => {
        this.roleOptions = response.data;
      });
      this.getList();
    },
    methods: {
      handleOffLine(row){
        let _this=this;
        this.$confirm(`确定要用户昵称为[${row.nickname}],手机号码为[${row.mobile}]强制下线?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          offline({id:row.userId,role:row.role}).then(res => {
            console.log(res);
            _this.msgSuccess("强制下线成功");
            _this.getList();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      referenceInfo(userId) {
        this.title = '用户详情';
        list({userId: userId}).then(response => {
          this.userInfoOpen = true;
          this.user = response.rows[0];
        });
      },
      remoteMethod(query) {
        if (query && query.trim()) {
          this.selectLoading = true;
          list({mobile: query, selfMobile: this.form.mobile}).then(response => {
            console.log(response);
            this.options = response.rows;
            this.selectLoading = false;
          });
        }
      },
      getList() {
        this.loading = true;
        list(this.queryDateRange(this.queryParams,this.startTime)).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
          console.log(this.postList);
        });
      },
      // 字典状态字典翻译
      genderFormat(row, column) {
        return this.selectDictLabel(this.genderOptions, row.gender);
      },
      // 状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      roleFormat(row, column) {
        return this.selectDictLabel(this.roleOptions, row.role);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      reset() {
        this.form = {
          singleamount: 150,
          userId: undefined,
          number: 1,
          role: undefined,
          mobile: undefined,
          inviteUserId: undefined,
          inviteNickname: undefined
        };
        this.resetForm("form");
      },
      handleInsert(row) {
        this.reset();

        this.title = "添加创客";
        this.form.userId = row.userId;
        this.form.inviteNickname = row.inviteNickname;
        this.form.inviteMobile = row.inviteMobile;
        this.form.inviteRole=this.selectDictLabel(this.roleOptions,row.inviteRole )+ "端";
        this.form.role = this.selectDictLabel(this.roleOptions, row.role) + "端";
        this.form.mobile = row.mobile;

        this.open = true;
      },
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            addMaker(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        });
      },
      cancel() {
        this.open = false;
        this.reset();
      }
    }
  };
</script>
